package zhihui.mobile.presenter.shop;

import android.content.Context;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.shop.ShopAttentionManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.attention.IShopAttentionContract;

/**
 * Created by 张公铭 on 2017/8/15 0015.
 */

public class ShopAttentionPresenter extends BaseShopPresenter implements IShopAttentionContract.IShopAttentionPresenter
{
    private Context mContext;
    private IShopAttentionContract.IShopAttentionView attentionView;
    private List<Integer> list = new ArrayList<>();

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        attentionView = (IShopAttentionContract.IShopAttentionView) view;
    }

    @Override
    public void onResume()
    {
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void addCheck(int spuId)
    {
        list.add(spuId);
    }

    @Override
    public void deleteCheck(int spuId)
    {
        for (int i = 0; i < list.size(); i++)
        {
            if (list.get(i) == spuId)
            {
                list.remove(i);
                return;
            }
        }
    }

    @Override
    public void cancelAttention()
    {
        if (list.size() <= 0)
        {
            return;
        }

        attentionView.showLoading();
        List<Object> values = new ArrayList<>();
        values.add(UserManager.getInstance().getCurUser().getId());
        StringBuffer sb = new StringBuffer();
        List<SpuInfo> spuInfos = new ArrayList<>();
        spuInfos.addAll(ShopAttentionManager.getInstance().getList());

        for (SpuInfo info : spuInfos)
        {
            if (list.contains(info.getId()))
            {
                ShopAttentionManager.getInstance().getList().remove(info);
            }
        }

        for (int i = 0; i < list.size(); i++)
        {
            sb.append(list.get(i));
            if (i < (list.size() - 1))
            {
                sb.append(",");
            }
        }
        values.add(sb.toString());
        ShopAttentionManager.getInstance().delete(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                attentionView.hindLoading();
                attentionView.showMessage("取消关注成功！");
                attentionView.refreshView(ShopAttentionManager.getInstance().getList());
            }

            @Override
            public void failed(String message)
            {
                attentionView.hindLoading();
                attentionView.showMessage(message);
            }
        }, values);

    }

    @Override
    public void initViewData()
    {
        attentionView.refreshView(ShopAttentionManager.getInstance().getList());
    }
}
